package com.susu.zookeeper.curator.component;

import com.susu.zookeeper.curator.enums.LockTypeEnum;
import com.susu.zookeeper.curator.model.ZookeeperLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * zookeeper锁服务
 *
 * @author 苏苏
 * @date 2020/11/6 10:47
 **/
@Slf4j
@Component
public class ZookeeperComponent {

    @Autowired
    private CuratorFramework curatorFramework;

    /**
     * 锁路径前缀
     */
    @Value("${lock.prefix.path}")
    private String bathPath;


    /**
     * 创建锁
     *
     * @param lockTypeEnum 锁类型
     * @param key          key
     * @return 锁类
     */
    public ZookeeperLock createZookeeperLock(LockTypeEnum lockTypeEnum, String key) {
        return new ZookeeperLock(curatorFramework, bathPath + key, lockTypeEnum);
    }
}
